import { defineConfig, UserConfig, ConfigEnv, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import postcsspxtorem from "postcss-pxtorem";
import path from "path";
import seoPrerender from "vite-plugin-seo-prerender";
import { VantResolver } from "@vant/auto-import-resolver";

export default defineConfig(({ mode }: ConfigEnv): UserConfig => {
  // vite.config.ts 只有在开发模式才能看到，生产模式用不到这些设置
  // 环境变量需要这样去获取
  const env = loadEnv(mode, process.cwd());
  console.log(env.VITE_APP_API_URL);
  return {
    plugins: [
      vue(),
      AutoImport({
        imports: ["vue", "vue-router"],
        dts: "src/auto-imports.d.ts",
      }),
      Components({
        resolvers: [VantResolver()],
        dts: "src/components.d.ts",
      }),
      seoPrerender({
        hashHistory: true, // 使用hash路由，需设置为true
        routes: ["/"], // 需要生成的路由
      }),
    ],
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "./src"),
      },
    },
    css: {
      loaderOptions: {
        less: {
          math: "always",
          globalVars: {
            mainColor: "red",
          },
        },
      },
      postcss: {
        plugins: [
          postcsspxtorem({
            rootValue: 37.5,
            propList: ["*"],
          }),
        ],
      },
    },
    build: {
      outDir: "dist",
      assetsDir: "assets",
      rollupOptions: {
        output: {
          manualChunks: undefined,
        },
      },
    },
    server: {
      // port: process.env.VITE_PORT || 3000, // 使用环境变量 VITE_PORT, 默认 3000
      proxy: {
        "/dapp": {
          target: env.VITE_APP_API_URL || "", // 目标服务器地址
          changeOrigin: true, // 是否改变请求头中的 origin
          rewrite: (path) => path.replace(/^\/dapp/, ""), // 重写路径
        },
      },
    },
<<<<<<< HEAD
  };
=======
  },
  server: {
    // port: process.env.VITE_PORT || 3000, // 使用环境变量 VITE_PORT, 默认 3000
    /*
      axios 的二次封装可以配合着这个就可以完成，跨域代理
    这里的意思 /api：就是目标服务器的地址
    代理了过后在浏览器会看到：http://localhost:5173/api/dapp/index/checkUser 【/api：就是http://admintest.dayoulove.com】，vite会进行转发到
    去除/api:[rewrite: (path) => path.replace(/^\/api/, ""), // 重写路径]
    http://admintest.dayoulove.com/dapp/index/checkUser
    */
    proxy: {
      "/dapp": {
        target: process.env.VITE_APP_API_URL || "", // 目标服务器地址
        changeOrigin: true, // 是否改变请求头中的 origin
        rewrite: (path) => path.replace(/^\/dapp/, ""), // 重写路径
      },
    },
  },
>>>>>>> 694aa06701711625ef411ee74089f4c6049f213e
});
